import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../usecase/search_usecase.dart';
import '../usecase/usecase.dart';

abstract class SearchController implements Controller {
  final SearchPresenter _presenter;
  final NovelRepository _novelRepository;
  final CacheRepository _cacheRepository;

  SearchController(
      this._presenter, this._novelRepository, this._cacheRepository);

  Future<void> searchByText(String value) async => SearchByTextUseCase(
      _presenter, _novelRepository, _cacheRepository)(value);
  Future<void> clearSearchHistory() async =>
      ClearSearchHistoryUseCase(_presenter, _cacheRepository)(NoParams());
}

class ClearSearchHistoryUseCase extends UseCase<void, NoParams> {
  final SearchPresenter _presenter;
  final CacheRepository _cacheRepository;

  ClearSearchHistoryUseCase(this._presenter, this._cacheRepository);

  @override
  void call(NoParams params) {
    _cacheRepository.set(I18n.search_history, '');
    _presenter.searchList = [];
    _presenter.onComplete();
  }
}
